package com.gitee.dupe.oauth.granter;

import com.gitee.dolt.core.util.BizAssert;
import com.gitee.dolt.security.model.AuthInfo;
import com.gitee.dolt.security.model.SysUser;
import com.gitee.dolt.security.token.TokenProvider;
import com.gitee.dupe.authority.entity.User;
import com.gitee.dupe.authority.service.IResourceService;
import com.gitee.dupe.authority.service.IRoleService;
import com.gitee.dupe.authority.service.IUserService;
import com.gitee.dupe.oauth.dto.LoginDTO;
import org.springframework.stereotype.Component;


/**
 * RefreshTokenGranter
 *
 * @author Dave Syer
 * @author LYH
 */
@Component(RefreshTokenGranter.GRANT_TYPE)
public class RefreshTokenGranter extends AbstractTokenGranter implements TokenGranter {

    public static final String GRANT_TYPE = "REFRESH_TOKEN";
    private final IUserService userService;

    public RefreshTokenGranter(IUserService userService, TokenProvider tokenProvider, IRoleService roleService, IResourceService resourceService) {
        super(tokenProvider, roleService, resourceService);
        this.userService = userService;
    }

    @Override
    public AuthInfo grant(LoginDTO loginDTO) {
        String token = loginDTO.getToken();
        BizAssert.notEmpty(token, "token不能为空");
        AuthInfo authInfo = tokenProvider.getAuthInfo(token);
        SysUser sysUser = authInfo.getUser();
        Long id = sysUser.getId();
        User user = userService.getById(id);
        authInfo = createAuthInfo(user);
        return authInfo;
    }
}
